#!/usr/bin/perl
use strict;
use modules::encode;
package osregchan;

sub osregchan {
    my ($dbh,$remote,$input,$osnum,$csnum) = @_;
    my $sth; my $auth;
    if ($$input[4] eq "") {
	print $remote "$osnum O $$input[0] :Syntax :REGCHAN <channel> <nick>\n";
    }
    else {
	$sth=$dbh->prepare("SELECT auth FROM ninfo WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."';");
	$sth->execute;
	$auth=$sth->fetchrow_array;

	$sth=$dbh->prepare("SELECT cslvl FROM nicks WHERE rnick = '$auth';");
	$sth->execute;
       
	if ($sth->fetchrow_array < 600) {
	    print $remote "$osnum O $$input[0] :Access denied.\n";
	}
	else {
	    $sth=$dbh->prepare("SELECT rnick FROM nicks WHERE rnick = '$$input[5]';");
	    $sth->execute;
	    if (!$sth->fetchrow_array) {
		print $remote "$osnum O $$input[0] :Rnick not registered.\n";
	    }
	    else {
		$sth=$dbh->prepare("SELECT channel FROM channels WHERE channel = '$$input[4]';");
		$sth->execute;
		if ($sth->fetchrow_array) {
		    print $remote "$osnum O $$input[0] :Channel already registered.\n";
		}
		else {
		    my $time = time;
		    $sth=$dbh->prepare("INSERT INTO channels values ('$$input[4]','$$input[5]','Channel Registered at $time','nt',0,0,0,'$time','0','$time');");
		    $sth->execute;
		    print $remote "$csnum J $$input[4]\n";
		    my $num = $csnum;
		    chop $num; chop $num; chop $num;
		    print $remote "$num M $$input[4] +o $csnum\n";
		}
	    }
	}
    }
}
1;
